package member.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.model.service.MemberService;
import member.to.ActionForward;
import member.to.MemberTO;

public class SearchMemberByMileageRangeController implements Controller {
	
	@Override
	public ActionForward handle(HttpServletRequest request,
			HttpServletResponse response) {
		ActionForward af = null;
		String sm = request.getParameter("sMileage");
		String em = request.getParameter("eMileage");
		int sMileage = 0;
		int eMileage = 0;
		try {
			sMileage = Integer.parseInt(sm);
			eMileage = Integer.parseInt(em);
			//Business Logic
			MemberService mms = MemberService.getInstance();
			ArrayList<MemberTO > list = mms.searchMemberByMileageRange(sMileage, eMileage);
			request.setAttribute("member_list", list);
			request.setAttribute("title", "마일리지 범위로 조회 결과");
			request.setAttribute("search_value", sm+"~"+em);
			af = new ActionForward("/member_list.jsp", false);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			request.setAttribute("error_message","올바르지 않은 검색조건 입니다.");
			af = new ActionForward("/search_form.jsp", false);
			
		} catch (SQLException e) {
			e.printStackTrace();
			request.setAttribute("error_message", e.getMessage());
			af = new ActionForward("/error.jsp", false);
		}
		return af;
	}

}
